package com.yao.koin

import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.yao.koin.adapter.ActivityGrilAdapter
import com.yao.koin.base.BaseVMActivity
import com.yao.koin.bean.BannerChild
import com.yao.koin.databinding.ActivityMainBinding
import com.yao.koin.ktx.dp
import com.yao.koin.ktx.dp2px
import com.yao.koin.ktx.toast
import com.yao.koin.ui.ActivityViewModel
import com.yao.koin.util.GlideImageLoader
import com.yao.koin.view.CustomLoadMoreView
import com.yao.koin.view.SpaceItemDecoration
import com.youth.banner.BannerConfig
import org.koin.androidx.viewmodel.ext.android.viewModel

class MainActivity : BaseVMActivity() {


//    private val articleViewModel: ActivityViewModel by viewModel()

    private val articleViewModel by viewModel<ActivityViewModel>()

    private val binding by binding<ActivityMainBinding>(R.layout.activity_main)

    private val activityAdapter by lazy { ActivityGrilAdapter() }

    private val bannerImages = mutableListOf<String>()
    private val bannerTitles = mutableListOf<String>()
    private val bannerUrls = mutableListOf<String>()
    private val banner by lazy { com.youth.banner.Banner(this) }




    override fun initView() {

        binding.run {
            viewModel = articleViewModel
//           adapter = activityAdapter
        }

        binding.homeRecycleView.run {
            layoutManager = LinearLayoutManager(this@MainActivity)
            addItemDecoration(SpaceItemDecoration(bottom = 20.dp.toInt()))
        }

        initRecycleView()
        initBanner()

    }

    override fun initData() {
        refresh()
    }

    override fun startObserve() {

        articleViewModel.apply {

            mBanners.observe(this@MainActivity, Observer { it ->
                it?.let { setBanner(it) }
            })



            uiState.observe(this@MainActivity, Observer {

                it.showSuccess?.let { list ->
                    activityAdapter.setEnableLoadMore(false)
//                    list.data.forEach { it.collect = true }
                    activityAdapter.run {

                        val nide = list;

                        if (it.isRefresh) replaceData(list)
                        else addData(list)
                        setEnableLoadMore(true)
                        loadMoreComplete()


                    }

//                    toast(list[0].images[0].toString())
                }

                if (it.showEnd) activityAdapter.loadMoreEnd()

                it.showError?.let { message ->
                    toast(if (message.isBlank()) "网络异常" else message)
                }
            })
        }


    }


    private fun refresh() {
        articleViewModel.getHomeGirlList(true)
    }

    private fun loadMore() {
        articleViewModel.getHomeGirlList(false)
    }

    private fun initRecycleView() {
        activityAdapter.run {
//            setOnItemClickListener { _, _, position ->
//                val bundle = bundleOf(BrowserActivity.URL to homeArticleAdapter.data[position].link)
//                NavHostFragment.findNavController(this@HomeFragment).navigate(R.id.action_tab_to_browser, bundle)
//            }
            onItemChildClickListener = this@MainActivity.onItemChildClickListener
            if (headerLayoutCount > 0) removeAllHeaderView()
            addHeaderView(banner)
            setLoadMoreView(CustomLoadMoreView())
            setOnLoadMoreListener({ loadMore() }, binding.homeRecycleView)
        }


        binding.homeRecycleView.adapter = activityAdapter

    }
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        when (view.id) {
            R.id.articleImg -> {
                    activityAdapter.run {
                        data[position].run {
                            toast(data[position].desc +",你点了" + position,0)
                        }
//                        notifyDataSetChanged()
                    }
            }
        }
    }


    private fun initBanner() {

        banner.run {
            layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, banner.dp2px(200))
            setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE)
            setImageLoader(GlideImageLoader())
//            setOnBannerListener { position ->
//                run {
//                    Navigation.findNavController(banner).navigate(R.id.action_tab_to_browser, bundleOf(BrowserActivity.URL to bannerUrls[position]))
//                }
//            }
        }
    }


    private fun setBanner(bannerList: List<BannerChild>) {
        for (banner in bannerList) {
            bannerImages.add(banner.image)
            bannerTitles.add(banner.title)
            bannerUrls.add(banner.url)
        }
        banner.setImages(bannerImages)
            .setBannerTitles(bannerTitles)
            .setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE)
            .setDelayTime(3000)
        banner.start()
    }

    override fun onStart() {
        super.onStart()
        banner.startAutoPlay()
    }

    override fun onStop() {
        super.onStop()
        banner.stopAutoPlay()
    }


}